<!-- 审核表单 -->

<template>
  <el-dialog
    :model-value="dialogFormVisible"
    :destroy-on-close="true"
    title="权限组信息"
    width="450px"
    @close="emits('closeDialog')"
  >
    <el-form
      ref="dialogFormRef"
      :model="dialogForm"
      :rules="rules"
      :label-width="formLabelWidth"
    >
      <el-form-item label="项目名称" prop="project_name">
        <el-select v-model="dialogForm.project_name" placeholder="请选择">
          <el-option
            v-for="item in projectOptions"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- <el-form-item label="项目代码" prop="project_code">
        <el-select v-model="dialogForm.project_code" placeholder="请选择">
          <el-option v-for="item in projectOptions" :key="item.code" :label="item.name" :value="item.code"> </el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item label="菜单名称" prop="menuList">
        <el-cascader v-model="dialogForm.menuList" :options="permissionMenu" />
      </el-form-item>

      <el-form-item label="操作方法" prop="method">
        <el-checkbox-group v-model="dialogForm.method">
          <template v-for="(item, index) of methodList">
            <el-checkbox :label="item.method">{{ item.name }}</el-checkbox>
          </template>
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="后端api名称" prop="api_name">
        <el-input v-model="dialogForm.api_name" />
      </el-form-item>

      <el-form-item label="排序数字">
        <el-input v-model="dialogForm.priority" />
      </el-form-item>

      <el-form-item label="id" style="display: none">
        <el-input v-model="dialogForm.id" disabled />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="emits('cancel')">取消</el-button>
        <el-button
          type="primary"
          @click="submit(dialogForm)"
          :loading="btnLoading"
        >
          提交
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref, onMounted } from "vue";
import { permissionMenu, permissionMenuRes } from "@/config/permissionMenu";
import { projectApi } from "@/api/system/system";

onMounted(() => {
  getTbaleData();
});

const dialogFormRef = ref(null);
const projectOptions = ref([]);
const param = () => {
  return {};
};
// 获取表格数据
const getTbaleData = async () => {
  try {
    const res = await projectApi(param());
    projectOptions.value = res.data;
  } catch (error) {
    return false;
  }
};
defineProps({
  dialogFormVisible: {
    type: Boolean,
    default: false,
  },
  methodList: {
    type: Array,
    default: [],
  },
  dialogForm: {
    type: Object,
    default: {
      id: "",
      project_name: "",
      project_code: "",
      method: [],
      api_name: "",
      priority: "",
      menuList: [],
    },
  },
  btnLoading: {
    type: Boolean,
    default: false,
  },
});
const emits = defineEmits(["closeDialog", "cancel", "submitForm"]);

const formLabelWidth = "120px";
// 校验
// 校验
const rules = {
  menuList: [{ required: true, message: "请选择菜单名称", trigger: "blur" }],
  project_name: [
    { required: true, message: "请输入项目名称", trigger: "blur" },
  ],
  // project_code: [{ required: true, message: '请输入项目代码', trigger: 'blur' }],
  api_name: [{ required: true, message: "请输入后端api名称", trigger: "blur" }],
  method: [{ required: true, message: "请选择操作方法", trigger: "blur" }],
};
// 提交
const submit = (dialogForm) => {
  dialogFormRef.value
    .validate()
    .then(async () => {
      try {
        const param = Object.assign({}, dialogForm);
        let length = param.menuList.length - 1;
        param.method = param.method.join(",");
        console.log("1", param.menuList[length]);
        console.log("2", permissionMenuRes[param.menuList[length]]);
        param.name = permissionMenuRes[param.menuList[length]];
        param.code = param.menuList[length];
        param.project_code = param.project_name;
        param.parent_code = length == 2 ? param.menuList[1] : param.menuList[0];
        delete param.menuList;
        emits("submitForm", param);
      } catch (error) {
        return false;
      }
    })
    .catch((error) => {
      return false;
    });
};
</script>
